package org.example.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.mapper.HealthMapper;
import org.example.pojo.Health;
import org.example.service.HealthService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: HealthServiceImpl
 * Description:
 *
 * @Author Kotaro
 * @Create 2024/12/4 17:07
 * @Version 1.0
 */
@Service
public class HealthServiceImpl implements HealthService {

    @Resource
    private HealthMapper healthMapper;

    @Override
    public JSONObject getBloodGlucoseData(String elderlyId) {
        // 假设血糖数据存储在 List<Health> 中
        List<Health> bloodGlucose = healthMapper.getBloodGlucose(elderlyId);

        JSONObject result = new JSONObject();
        JSONArray dataArray = new JSONArray();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 按日期分组
        Map<String, List<Health>> groupedByDate = new HashMap<>();
        for (Health health : bloodGlucose) {
            String date = dateFormat.format(health.getMeasurementTime());
            groupedByDate.computeIfAbsent(date, k -> new ArrayList<>()).add(health);
        }

        // 处理每个日期的血糖数据
        for (Map.Entry<String, List<Health>> entry : groupedByDate.entrySet()) {
            String date = entry.getKey();
            List<Health> healthList = entry.getValue();

            // 计算每日的平均血糖
            BigDecimal totalGlucose = BigDecimal.ZERO;
            for (Health health : healthList) {
                totalGlucose = totalGlucose.add(health.getBloodGlucose());
            }
            BigDecimal averageGlucose = totalGlucose.divide(new BigDecimal(healthList.size()), 2, RoundingMode.HALF_UP);

            // 构建当天的数据
            JSONObject dayData = new JSONObject();
            JSONArray glucoseData = new JSONArray();
            for (Health health : healthList) {
                JSONObject data = new JSONObject();
                data.put("measurementTime", dateTimeFormat.format(health.getMeasurementTime()));
                data.put("glucoseValue", health.getBloodGlucose());
                glucoseData.add(data);
            }

            // 以当天的第一个测量时间作为日期
            dayData.put("date", date);  // 设置日期字段
            dayData.put("glucose", glucoseData);
            dayData.put("averageGlucose", averageGlucose);

            dataArray.add(dayData);
        }

        // 最终将 data 放入 result 中
        result.put("data", dataArray);
        return result;
    }
    public JSONObject getBloodPressureData(String elderlyId) {
        // 查询血压数据
        List<Health> bloodPressure = healthMapper.getBloodPressure(elderlyId);

        // 设置数据格式
        JSONObject result = new JSONObject();
        JSONArray dataArray = new JSONArray(); // 存放分组后的数据

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 按日期分组
        Map<String, List<Health>> groupedByDate = new HashMap<>();
        for (Health health : bloodPressure) {
            // 获取日期（忽略时间）
            String date = dateFormat.format(health.getMeasurementTime());
            groupedByDate.computeIfAbsent(date, k -> new ArrayList<>()).add(health);
        }

        // 处理每个日期的血压数据
        for (Map.Entry<String, List<Health>> entry : groupedByDate.entrySet()) {
            String date = entry.getKey();
            List<Health> healthList = entry.getValue();

            // 计算每日的高压和低压平均值
            BigDecimal totalHighPressure = BigDecimal.ZERO;
            BigDecimal totalLowPressure = BigDecimal.ZERO;

            for (Health health : healthList) {
                totalHighPressure = totalHighPressure.add(new BigDecimal(health.getHighPressure()));
                totalLowPressure = totalLowPressure.add(new BigDecimal(health.getLowPressure()));
            }

            BigDecimal averageHighPressure = totalHighPressure.divide(new BigDecimal(healthList.size()), 2, RoundingMode.HALF_UP);
            BigDecimal averageLowPressure = totalLowPressure.divide(new BigDecimal(healthList.size()), 2, RoundingMode.HALF_UP);

            // 构建当天的数据
            JSONObject dayData = new JSONObject();
            JSONArray pressureData = new JSONArray();

            for (Health health : healthList) {
                JSONObject data = new JSONObject();
                data.put("measurementTime", dateTimeFormat.format(health.getMeasurementTime()));
                data.put("highPressure", health.getHighPressure());
                data.put("lowPressure", health.getLowPressure());
                pressureData.add(data);
            }

            // 添加日期、平均高压、平均低压以及血压数据到当天的 JSON 对象
            dayData.put("date", date);  // 设置日期字段
            dayData.put("highPressure", averageHighPressure); // 设置平均高压
            dayData.put("lowPressure", averageLowPressure); // 设置平均低压
            dayData.put("bloodPressure", pressureData); // 设置当天的血压数据

            // 将当天的数据添加到最终结果中
            dataArray.add(dayData);
        }

        // 最终将 data 放入 result 中
        result.put("data", dataArray);
        return result;
    }
}
